package com.anolesoft.yr.mbus.part.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.part.dto.PartTypeDTO;
import com.anolesoft.yr.mbus.part.service.PartTypeService;
import com.anolesoft.yr.mbus.part.vo.PartTypeVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/11 11:52
 */
@RestController
public class PartTypeController {
    @Autowired
    private PartTypeService partTypeService;

//    private PartTypeVO dto2vo(PartTypeDTO partTypeDTO) {
//        return PartTypeVO.builder()
//                .createTime(partTypeDTO.getCreateTime())
//                .createUserUid(partTypeDTO.getCreateUserUid())
//                .deleteFlag(partTypeDTO.getDeleteFlag())
//                .factoryCode(partTypeDTO.getFactoryCode())
//                .modifyTime(partTypeDTO.getModifyTime())
//                .modifyUserUid(partTypeDTO.getModifyUserUid())
//                .objuid(partTypeDTO.getObjuid())
//                .remark(partTypeDTO.getRemark())
//                .typeCode(partTypeDTO.getTypeCode())
//                .typeName(partTypeDTO.getTypeName())
//                .build();
//    }
    private PartTypeDTO vo2dto(PartTypeVO partTypeVO, boolean newId) {
        return PartTypeDTO.builder()
                .factoryCode(partTypeVO.getFactoryCode())
                .modifyUserUid(SecurityUtils.getUserId())
                .remark(partTypeVO.getRemark())
                .objuid(newId ? CommonUtils.fastUUID() : partTypeVO.getObjuid())
                .createUserUid(SecurityUtils.getUserId())
                .typeName(partTypeVO.getTypeName())
                .typeCode(partTypeVO.getTypeCode())
                .build();
    }

    @PostMapping(value = "/api/part/parttype/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(PartTypeVO PartTypeVO) {
        PartTypeDTO partTypeDTO = vo2dto(PartTypeVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, partTypeService.create(partTypeDTO));
    }


    @PutMapping(value = "/api/part/parttype/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(PartTypeVO PartTypeVO) {
        PartTypeDTO partTypeDTO = vo2dto(PartTypeVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, partTypeService.modify(partTypeDTO));
    }

    @DeleteMapping(value = "/api/part/parttype/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, partTypeService.remove(id));
    }

    @DeleteMapping(value = "/api/part/parttype/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, partTypeService.removeBatch(ids));
    }

    @GetMapping(value = "/api/part/parttype/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<PartTypeDTO> partTypeDTO = this.partTypeService.selectOneById(id);
        if (partTypeDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.partTypeDTO2VO(partTypeDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }

    }

    @GetMapping(value = "/api/part/parttype/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<PartTypeVO> vos = partTypeService.selectAll().stream().map(t -> CustomBeanUtils.partTypeDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartTypeVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/parttype/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
//        List<PartTypeVO> vos = partTypeService.selectAll(limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
//        PageInfo<List<PartTypeVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        PageData<List<PartTypeDTO>> pageData = partTypeService.selectAll(limit, curr);
        List<PartTypeVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.partTypeDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartTypeVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/parttype/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(PartTypeVO PartTypeVO) {
        PartTypeDTO partTypeDTO = vo2dto(PartTypeVO, false);
        List<PartTypeVO> vos = this.partTypeService.selectList(partTypeDTO).stream().map(t -> CustomBeanUtils.partTypeDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartTypeVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/parttype/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(PartTypeVO partTypeVO) {
        PartTypeDTO partTypeDTO = vo2dto(partTypeVO, false);
        Integer limit = partTypeVO.getRows();
        Integer curr = partTypeVO.getPage();
//        List<PartTypeVO> vos = this.partTypeService.selectList(partTypeDTO, limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
//        PageInfo<List<PartTypeVO>> pageInfo = new PageInfo<>(limit, vos.size(), vos);
        PageData<List<PartTypeDTO>> pageData = partTypeService.selectList(partTypeDTO, limit, curr);
        List<PartTypeVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.partTypeDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartTypeVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/api/part/parttype/list/combox", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectComboxList(@RequestBody PartTypeVO partTypeVO) {
        PartTypeDTO partTypeDTO = vo2dto(partTypeVO, false);
        Integer limit = partTypeVO.getRows();
        Integer curr = partTypeVO.getPage();
        PageData<List<PartTypeDTO>> pageData = partTypeService.selectComboxList(partTypeDTO, limit, curr);
        List<PartTypeVO> vos = pageData.getItems().stream().map(CustomBeanUtils::partTypeDTO2VO).collect(Collectors.toList());
        PageInfo<List<PartTypeVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }
}
